View Javadoc
1 /* file name : InvokeSpecial.java 2 * authors : Christopher Ellsworth (Chris@chrisellsworth.com) 3 * Clarence Alston 4 * created : 11/15/2002 12:10:39 5 * 6 * modifications: 7 * 8 */ 9 package jrre.instructionset.methodinvocation; 10 11 import jrre.*; 12 import jrre.Stack; 13 import jrre.StackFrame; 14 import jrre.MethodArea; 15 import jrre.types.*; 16 import jrre.api.java.lang.reflect.DescriptorDecoder; 17 import jrre.classloader.classfile.pool_entries.*; 18 19 /*** 20 * Represents and contains execution tasks for the <code>invokespecial</code> 21 * instruction. This is used when calling the constructor of a method. 22 * 23 * @author Christopher Ellsworth (Chris@chrisellsworth.com) 24 * @author Clarence Alston 25 */ 26 public class InvokeVirtual extends jrre.instructionset.Instruction { 27 28 private int operandOne; 29 private int operandTwo; 30 31 public InvokeVirtual(int operandOne, int operandTwo){ 32 this.operandOne = operandOne; 33 this.operandTwo = operandTwo; 34 35 length = 2; 36 } 37 38 /*** 39 * Executed the <code>invokevirtual</code> instruction on the 40 * specified stack frame. 41 */ 42 public void execute(){ 43 44 int cpMethodIndex = (operandOne << 8) | operandTwo; 45 46 // pop object and get methodref from objects symbol table. 47 jrre.api.java.lang.Class thisClass = Stack.getClassContainingMethod(); 48 49 CPMethodRef methodRef = (CPMethodRef)thisClass.getSymbol(cpMethodIndex); 50 51 CPClass cpClass = (CPClass)thisClass.getSymbol(methodRef.getClassIndex()); 52 CPUTF8 cpClassUTF8 = (CPUTF8)thisClass.getSymbol(cpClass.getNameIndex()); 53 54 CPNameType methodNameType = (CPNameType)thisClass.getSymbol(methodRef.getNameTypeIndex()); 55 56 String methodName = ((CPUTF8)thisClass.getSymbol(methodNameType.getNameIndex())).getValue(); 57 String methodType = ((CPUTF8)thisClass.getSymbol(methodNameType.getDescriptorIndex())).getValue(); 58 59 methodName += methodType; 60 61 jrre.api.java.lang.Class methodClass = MethodArea.getClass(cpClassUTF8.getValue()); 62 63 //System.out.println(methodClass.getFullyQualifiedName()); 64 //System.out.println("MethodName: "+methodName); 65 66 jrre.api.java.lang.reflect.MethodEntry method = methodClass.getMethod(methodName); 67 68 // For JRRE system.out.println(); 69 // To be removed when natives are implemented. 70 // Print charecter. 71 if((methodName.startsWith("print(C)")) && 72 (methodClass.getFullyQualifiedName().equals("jrre/api/java/io/OutputStream"))){ 73 74 Type operandToPrint = Stack.popOperand(); 75 76 if(operandToPrint instanceof CharType) 77 System.out.print((char)((CharType)operandToPrint).getValue()); 78 else if(operandToPrint instanceof ReferenceType){ 79 System.out.print("Trying to print object to a charecter?"); 80 } 81 return; 82 } 83 if((methodName.startsWith("println(C)")) && 84 (methodClass.getFullyQualifiedName().equals("jrre/api/java/io/OutputStream"))){ 85 86 Type operandToPrint = Stack.popOperand(); 87 88 if(operandToPrint instanceof CharType) 89 System.out.println((char)((CharType)operandToPrint).getValue()); 90 else if(operandToPrint instanceof ReferenceType){ 91 System.out.println("Trying to print object to a charecter?"); 92 } 93 return; 94 } 95 if((methodName.startsWith("println(Ljava/lang/String;)V")) && 96 (methodClass.getFullyQualifiedName().equals("jrre/api/java/io/OutputStream"))){ 97 98 Type operandToPrint = Stack.popOperand(); 99 ObjectInstance objectToPrint = ((ReferenceType)operandToPrint).getValue(); 100 101 ReferenceType arrayRef = (ReferenceType)objectToPrint.getInstanceValue(3); 102 ArrayInstance array = (ArrayInstance)arrayRef.getValue(); 103 104 String toPrint = new String(); 105 for(int i =0;i < array.getSize();i++){ 106 CharType character = (CharType)array.getElement(i); 107 if(character != null) 108 toPrint+=character.getValue(); 109 } 110 System.out.println(toPrint); 111 112 return; 113 } 114 else if((methodName.startsWith("print(Ljava/lang/String;)V")) && 115 (methodClass.getFullyQualifiedName().equals("jrre/api/java/io/OutputStream"))){ 116 117 Type operandToPrint = Stack.popOperand(); 118 ObjectInstance objectToPrint = ((ReferenceType)operandToPrint).getValue(); 119 120 ReferenceType arrayRef = (ReferenceType)objectToPrint.getInstanceValue(3); 121 ArrayInstance array = (ArrayInstance)arrayRef.getValue(); 122 123 String toPrint = new String(); 124 for(int i =0;i < array.getSize();i++){ 125 CharType character = (CharType)array.getElement(i); 126 if(character != null) 127 toPrint+=character.getValue(); 128 } 129 System.out.print(toPrint); 130 131 return; 132 } 133 else if((methodName.startsWith("println")) && 134 (methodClass.getFullyQualifiedName().equals("jrre/api/java/io/OutputStream"))){ 135 136 Type operandToPrint = Stack.popOperand(); 137 138 if(operandToPrint instanceof PrimitiveType) 139 System.out.println(((PrimitiveType)operandToPrint).getValue()); 140 else if(operandToPrint instanceof ReferenceType){ 141 jrre.ObjectInstance objectToPrint = ((ReferenceType)operandToPrint).getValue(); 142 143 System.out.println(((StringType)objectToPrint.getInstanceValue(0)).getValue()); 144 } 145 return; 146 } 147 else if((methodName.startsWith("print")) && 148 (methodClass.getFullyQualifiedName().equals("jrre/api/java/io/OutputStream"))){ 149 150 Type operandToPrint = Stack.popOperand(); 151 152 if(operandToPrint instanceof PrimitiveType) 153 System.out.print(((PrimitiveType)operandToPrint).getValue()); 154 else if(operandToPrint instanceof ReferenceType){ 155 jrre.ObjectInstance objectToPrint = ((ReferenceType)operandToPrint).getValue(); 156 System.out.print(((StringType)objectToPrint.getInstanceValue(0)).getValue()); 157 } 158 return; 159 } 160 // To be removed when natives are implemented. ^^^^^^ 161 // To be removed when natives are implemented. 162 // To be removed when natives are implemented. 163 // To be removed when natives are implemented. 164 // To be removed when natives are implemented. 165 166 StackFrame newStackFrame = method.getStackFrame(); 167 168 newStackFrame.setClassContainingMethod(methodClass); 169 170 // get parameters. 171 String descriptor = method.getDescriptor(); 172 //System.out.println("Des: "+descriptor); 173 java.util.Iterator methodTypeIterator = DescriptorDecoder.getMethodTypes(descriptor); 174 175 //int localVariableIndex = 1; 176 int localVariableIndex = DescriptorDecoder.getParameterCount(); 177 178 //newStackFrame.setLocalVariable(localVariableIndex++, objectReference); 179 180 // get return type. 181 methodTypeIterator.next(); 182 183 while(methodTypeIterator.hasNext()){ 184 newStackFrame.setLocalVariable(localVariableIndex--, Stack.popOperand()); 185 if(jrre.JRRE.getVerbose()) 186 System.out.println("\tparam: "+newStackFrame.getLocalVariable(localVariableIndex+1)); 187 methodTypeIterator.next(); 188 } 189 190 // Set the objectRef to local variable 0. 191 newStackFrame.setLocalVariable(0, Stack.popOperand()); 192 if(jrre.JRRE.getVerbose()) 193 System.out.println("\tobj: "+newStackFrame.getLocalVariable(localVariableIndex)); 194 195 196 197 if(method.getMethodAccessFlags().isNative()){ 198 if(jrre.JRRE.getVerbose()) 199 System.out.println("Virtual Native: "+methodName); 200 System.out.println("In Class: "+methodClass.getFullyQualifiedName()); 201 202 jrre.NativeMethodArea.addMethod(methodName); 203 return; 204 } 205 206 207 208 Stack.push(newStackFrame, 209 cpClassUTF8.getValue()+"::"+ 210 method.getFullyQualifiedName()); 211 212 } 213 214 public String toString(){ 215 StringBuffer toReturn = new StringBuffer(); 216 toReturn.append("invokevirtual "); 217 toReturn.append(Integer.toHexString(operandOne)); 218 toReturn.append(" "); 219 toReturn.append(Integer.toHexString(operandTwo)); 220 221 return toReturn.toString(); 222 } 223 } 224

This page was automatically generated by Maven